package com.qcxy.gzy.activity.base;


import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.ViewGroup;

import com.qxcy.gzy.util.RelayoutTool;

public abstract class AdaptiveActivity extends Activity {

    protected static float scale = 0;
    private static final float UI_STANDARD_width = 1280;

    @Override
    public void setContentView(int layoutResID) {
        View view = View.inflate(this, layoutResID, null);
        this.setContentView(view);
    }

    @Override
    public void setContentView(View view) {
        if (scale == 0) {
            initScreenScale();
        }
        if (scale != 1) {
            RelayoutTool.relayoutViewHierarchy(view, scale);
        }
        super.setContentView(view);
    }

    @Override
    public void setContentView(View view, ViewGroup.LayoutParams params) {
        if (scale == 0) {
            initScreenScale();
        }
        RelayoutTool.relayoutViewHierarchy(view, scale);
        RelayoutTool.scaleLayoutParams(params, scale);
        super.setContentView(view, params);
    }

    public void initScreenScale() {
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        float width = displayMetrics.widthPixels;
        scale = width / UI_STANDARD_width;
    }
}
